package cn.leyaber.hrm.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//feign的拦截器，往请求头中添加token
@Component
public class DefaultOAuth2FeignRequestInterceptor implements RequestInterceptor {
    //请求token
    private final String AUTHORIZATION_HEADER = "Authorization";

    @Override
    public void apply(RequestTemplate template) {
        //获取请求对象
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        //请求对象
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader(AUTHORIZATION_HEADER);
        template.header(AUTHORIZATION_HEADER,token);

    }
}
